package com.lingyue.commons.utils;

import org.apache.commons.lang3.ObjectUtils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @Description: TODO
 * @Author: BaiJinhua
 * @Date: 2019/12/19 0:21
 * @See: com.lingyue.cpd.master.utils
 * @Version: 1.0
 * Copyright: Copyright (c) 2019
 */
public class DateHelper {

    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static final String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final SimpleDateFormat TIMESTAMP_FORMAT = new SimpleDateFormat(TIMESTAMP_PATTERN);

    public static final DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(TIMESTAMP_PATTERN);

    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN);

    private static Calendar getCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal;
    }

    /**
     * 获取今天之前几天零点时间
     * @param day
     * @return
     */
    public static String getTodayBefore(int day){
        Calendar cal = getCalendar();
        cal.set(Calendar.DATE, (cal.get(Calendar.DATE)-day));
        Date time = cal.getTime();
        return TIMESTAMP_FORMAT.format(time);
    }

    /**
     * 获取今天之后几天零点时间
     * @param day
     * @return
     */
    public static String getTodayAfter(int day){
        Calendar cal = getCalendar();
        cal.set(Calendar.DATE, (cal.get(Calendar.DATE)+day));
        Date time = cal.getTime();
        return TIMESTAMP_FORMAT.format(time);
    }

    /**
     * 获取今天之前几天零点时间
     * @param day
     * @return
     */
    public static String getTodayBefore(int day,String pattern){
        Calendar cal = getCalendar();
        cal.set(Calendar.DATE, (cal.get(Calendar.DATE)-day));
        Date time = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(time);
    }

    /**
     * 获取今天之后几天零点时间
     * @param day
     * @return
     */
    public static String getTodayAfter(int day,String pattern){
        Calendar cal = getCalendar();
        cal.set(Calendar.DATE, (cal.get(Calendar.DATE)+day));
        Date time = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(time);
    }

    public static Date localDateToDate(LocalDate localDate){
        if(ObjectUtils.isEmpty(localDate)) return null;
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay().atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        if(ObjectUtils.isEmpty(localDateTime)) return null;
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static LocalDate dateToLocalDate(Date date) {
        if(ObjectUtils.isEmpty(date)) return null;
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        if(ObjectUtils.isEmpty(date)) return null;
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    public static void main(String[] args) {
        String date = getTodayAfter(0);
        System.out.println(date);
    }
}
